package com.room.search.web.rest;

import com.room.search.repository.RoomUserBrokerChatListRepository;
import com.room.search.service.RoomUserBrokerChatListService;
import com.room.search.service.dto.RoomUserBrokerChatListDTO;
import com.room.search.web.rest.errors.BadRequestAlertException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import tech.jhipster.web.util.HeaderUtil;
import tech.jhipster.web.util.ResponseUtil;

/**
 * REST controller for managing {@link com.room.search.domain.RoomUserBrokerChatList}.
 */
@RestController
@RequestMapping("/api")
public class RoomUserBrokerChatListResource {

    private final Logger log = LoggerFactory.getLogger(RoomUserBrokerChatListResource.class);

    private static final String ENTITY_NAME = "roomUserBrokerChatList";

    @Value("${jhipster.clientApp.name}")
    private String applicationName;

    private final RoomUserBrokerChatListService roomUserBrokerChatListService;

    private final RoomUserBrokerChatListRepository roomUserBrokerChatListRepository;

    public RoomUserBrokerChatListResource(
        RoomUserBrokerChatListService roomUserBrokerChatListService,
        RoomUserBrokerChatListRepository roomUserBrokerChatListRepository
    ) {
        this.roomUserBrokerChatListService = roomUserBrokerChatListService;
        this.roomUserBrokerChatListRepository = roomUserBrokerChatListRepository;
    }

    /**
     * {@code POST  /room-user-broker-chat-lists} : Create a new roomUserBrokerChatList.
     *
     * @param roomUserBrokerChatListDTO the roomUserBrokerChatListDTO to create.
     * @return the {@link ResponseEntity} with status {@code 201 (Created)} and with body the new roomUserBrokerChatListDTO, or with status {@code 400 (Bad Request)} if the roomUserBrokerChatList has already an ID.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PostMapping("/room-user-broker-chat-lists")
    public ResponseEntity<RoomUserBrokerChatListDTO> createRoomUserBrokerChatList(
        @Valid @RequestBody RoomUserBrokerChatListDTO roomUserBrokerChatListDTO
    ) throws URISyntaxException {
        log.debug("REST request to save RoomUserBrokerChatList : {}", roomUserBrokerChatListDTO);
        if (roomUserBrokerChatListDTO.getId() != null) {
            throw new BadRequestAlertException("A new roomUserBrokerChatList cannot already have an ID", ENTITY_NAME, "idexists");
        }
        RoomUserBrokerChatListDTO result = roomUserBrokerChatListService.save(roomUserBrokerChatListDTO);
        return ResponseEntity
            .created(new URI("/api/room-user-broker-chat-lists/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(applicationName, false, ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * {@code PUT  /room-user-broker-chat-lists/:id} : Updates an existing roomUserBrokerChatList.
     *
     * @param id the id of the roomUserBrokerChatListDTO to save.
     * @param roomUserBrokerChatListDTO the roomUserBrokerChatListDTO to update.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the updated roomUserBrokerChatListDTO,
     * or with status {@code 400 (Bad Request)} if the roomUserBrokerChatListDTO is not valid,
     * or with status {@code 500 (Internal Server Error)} if the roomUserBrokerChatListDTO couldn't be updated.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PutMapping("/room-user-broker-chat-lists/{id}")
    public ResponseEntity<RoomUserBrokerChatListDTO> updateRoomUserBrokerChatList(
        @PathVariable(value = "id", required = false) final Long id,
        @Valid @RequestBody RoomUserBrokerChatListDTO roomUserBrokerChatListDTO
    ) throws URISyntaxException {
        log.debug("REST request to update RoomUserBrokerChatList : {}, {}", id, roomUserBrokerChatListDTO);
        if (roomUserBrokerChatListDTO.getId() == null) {
            throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
        }
        if (!Objects.equals(id, roomUserBrokerChatListDTO.getId())) {
            throw new BadRequestAlertException("Invalid ID", ENTITY_NAME, "idinvalid");
        }

        if (!roomUserBrokerChatListRepository.existsById(id)) {
            throw new BadRequestAlertException("Entity not found", ENTITY_NAME, "idnotfound");
        }

        RoomUserBrokerChatListDTO result = roomUserBrokerChatListService.save(roomUserBrokerChatListDTO);
        return ResponseEntity
            .ok()
            .headers(HeaderUtil.createEntityUpdateAlert(applicationName, false, ENTITY_NAME, roomUserBrokerChatListDTO.getId().toString()))
            .body(result);
    }

    /**
     * {@code PATCH  /room-user-broker-chat-lists/:id} : Partial updates given fields of an existing roomUserBrokerChatList, field will ignore if it is null
     *
     * @param id the id of the roomUserBrokerChatListDTO to save.
     * @param roomUserBrokerChatListDTO the roomUserBrokerChatListDTO to update.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the updated roomUserBrokerChatListDTO,
     * or with status {@code 400 (Bad Request)} if the roomUserBrokerChatListDTO is not valid,
     * or with status {@code 404 (Not Found)} if the roomUserBrokerChatListDTO is not found,
     * or with status {@code 500 (Internal Server Error)} if the roomUserBrokerChatListDTO couldn't be updated.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PatchMapping(value = "/room-user-broker-chat-lists/{id}", consumes = { "application/json", "application/merge-patch+json" })
    public ResponseEntity<RoomUserBrokerChatListDTO> partialUpdateRoomUserBrokerChatList(
        @PathVariable(value = "id", required = false) final Long id,
        @NotNull @RequestBody RoomUserBrokerChatListDTO roomUserBrokerChatListDTO
    ) throws URISyntaxException {
        log.debug("REST request to partial update RoomUserBrokerChatList partially : {}, {}", id, roomUserBrokerChatListDTO);
        if (roomUserBrokerChatListDTO.getId() == null) {
            throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
        }
        if (!Objects.equals(id, roomUserBrokerChatListDTO.getId())) {
            throw new BadRequestAlertException("Invalid ID", ENTITY_NAME, "idinvalid");
        }

        if (!roomUserBrokerChatListRepository.existsById(id)) {
            throw new BadRequestAlertException("Entity not found", ENTITY_NAME, "idnotfound");
        }

        Optional<RoomUserBrokerChatListDTO> result = roomUserBrokerChatListService.partialUpdate(roomUserBrokerChatListDTO);

        return ResponseUtil.wrapOrNotFound(
            result,
            HeaderUtil.createEntityUpdateAlert(applicationName, false, ENTITY_NAME, roomUserBrokerChatListDTO.getId().toString())
        );
    }

    /**
     * {@code GET  /room-user-broker-chat-lists} : get all the roomUserBrokerChatLists.
     *
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and the list of roomUserBrokerChatLists in body.
     */
    @GetMapping("/room-user-broker-chat-lists")
    public List<RoomUserBrokerChatListDTO> getAllRoomUserBrokerChatLists() {
        log.debug("REST request to get all RoomUserBrokerChatLists");
        return roomUserBrokerChatListService.findAll();
    }

    /**
     * {@code GET  /room-user-broker-chat-lists/:id} : get the "id" roomUserBrokerChatList.
     *
     * @param id the id of the roomUserBrokerChatListDTO to retrieve.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the roomUserBrokerChatListDTO, or with status {@code 404 (Not Found)}.
     */
    @GetMapping("/room-user-broker-chat-lists/{id}")
    public ResponseEntity<RoomUserBrokerChatListDTO> getRoomUserBrokerChatList(@PathVariable Long id) {
        log.debug("REST request to get RoomUserBrokerChatList : {}", id);
        Optional<RoomUserBrokerChatListDTO> roomUserBrokerChatListDTO = roomUserBrokerChatListService.findOne(id);
        return ResponseUtil.wrapOrNotFound(roomUserBrokerChatListDTO);
    }

    /**
     * {@code DELETE  /room-user-broker-chat-lists/:id} : delete the "id" roomUserBrokerChatList.
     *
     * @param id the id of the roomUserBrokerChatListDTO to delete.
     * @return the {@link ResponseEntity} with status {@code 204 (NO_CONTENT)}.
     */
    @DeleteMapping("/room-user-broker-chat-lists/{id}")
    public ResponseEntity<Void> deleteRoomUserBrokerChatList(@PathVariable Long id) {
        log.debug("REST request to delete RoomUserBrokerChatList : {}", id);
        roomUserBrokerChatListService.delete(id);
        return ResponseEntity
            .noContent()
            .headers(HeaderUtil.createEntityDeletionAlert(applicationName, false, ENTITY_NAME, id.toString()))
            .build();
    }
}
